package com.jiang.demo.depotstable.product;

import com.jiang.demo.depotstable.dds.datasource.DSContextHolder;
import com.jiang.demo.depotstable.domain.Descript;
import com.jiang.demo.depotstable.domain.Product;
import com.jiang.demo.depotstable.domain.Store;
import com.jiang.demo.depotstable.domain.Type;
import com.jiang.demo.depotstable.mapper.DescriptMapper;
import com.jiang.demo.depotstable.mapper.ProductMapper;
import com.jiang.demo.depotstable.mapper.StoreMapper;
import com.jiang.demo.depotstable.mapper.TypeMapper;
import com.jiang.demo.depotstable.type.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiangzhe
 * @create 2021/5/11 16:00
 */
@Slf4j
@Service
public class ProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    DescriptMapper descriptMapper;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    TypeMapper typeMapper;

    /**
     * 定时任务，每隔5s存储一条商品信息：
     * 1.根据随机的店铺 name 选择数据源(库)
     * 2.根据随机的类型 id 选择表
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void syncAddProduct(){
        DSContextHolder.setDBType("store-db");
        Random random = new Random();

        List<Store> storeList = storeMapper.getStoreInfos();
        if(storeList.size()<=0){
            log.warn("暂无店铺信息，请先添加");
            return;
        }
        int idx = random.nextInt(storeList.size());
        String name = storeList.get(idx).getName();
        int storeId = storeList.get(idx).getId();

        List<Type> typeList = typeMapper.getTypeInfos();
        if(typeList.size()<=0){
            log.warn("暂无商品类型信息，请先添加");
            return;
        }
        idx = random.nextInt(typeList.size());
        int typeId = typeList.get(idx).getId();

        Product product = new Product();
        product.setName("随机商品"+ Instant.now().getEpochSecond());
        product.setPrice(random.nextDouble());
        product.setType_id(typeId);
        product.setStore_id(storeId);
        addProduct(name, product);
        log.info("product-db-" + name + " add one: " + TypeService.tableName_product + typeId);

        Descript descript = new Descript();
        descript.setProduct_id(product.getId());
        descript.setDesc(product.getName()+"的描述");
        descript.setType_id(typeId);
        addDescript(name, descript);
        log.info("product-db-" + name + " add one: " + TypeService.tableName_descript + typeId);
    }

    // 添加商品
    public void addProduct(String storeName, Product product){
        DSContextHolder.setDBType("product-db-" + storeName);
        productMapper.insertProduct(product);
    }

    // 添加商品描述
    public void addDescript(String storeName, Descript descript){
        DSContextHolder.setDBType("product-db-" + storeName);
        descriptMapper.insertDescript(descript);
    }
}
